1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.base;
18
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.annotations.GwtIncompatible;
21 import com.google.common.testing.NullPointerTester;
22
23 import junit.framework.TestCase;
24
25
26
27
28
29
30 @GwtCompatible(emulated = true)
31 public class ObjectsTest extends TestCase {
32
33 public void testEqual() throws Exception {
34 assertTrue(Objects.equal(1, 1));
35 assertTrue(Objects.equal(null, null));
36
37
38 String s1 = "foobar";
39 String s2 = new String(s1);
40 assertTrue(Objects.equal(s1, s2));
41
42 assertFalse(Objects.equal(s1, null));
43 assertFalse(Objects.equal(null, s1));
44 assertFalse(Objects.equal("foo", "bar"));
45 assertFalse(Objects.equal("1", 1));
46 }
47
48 public void testHashCode() throws Exception {
49 int h1 = Objects.hashCode(1, "two", 3.0);
50 int h2 = Objects.hashCode(
51 new Integer(1), new String("two"), new Double(3.0));
52
53 assertEquals(h1, h2);
54
55
56 assertTrue(Objects.hashCode(1, 2, null) != Objects.hashCode(1, 2));
57 assertTrue(Objects.hashCode(1, 2, null) != Objects.hashCode(1, null, 2));
58 assertTrue(Objects.hashCode(1, null, 2) != Objects.hashCode(1, 2));
59 assertTrue(Objects.hashCode(1, 2, 3) != Objects.hashCode(3, 2, 1));
60 assertTrue(Objects.hashCode(1, 2, 3) != Objects.hashCode(2, 3, 1));
61 }
62
63 public void testFirstNonNull_withNonNull() throws Exception {
64 String s1 = "foo";
65 String s2 = Objects.firstNonNull(s1, "bar");
66 assertSame(s1, s2);
67
68 Long n1 = new Long(42);
69 Long n2 = Objects.firstNonNull(null, n1);
70 assertSame(n1, n2);
71 }
72
73 public void testFirstNonNull_throwsNullPointerException() throws Exception {
74 try {
75 Objects.firstNonNull(null, null);
76 fail("expected NullPointerException");
77 } catch (NullPointerException expected) {
78 }
79 }
80
81 @GwtIncompatible("NullPointerTester")
82 public void testNullPointers() {
83 NullPointerTester tester = new NullPointerTester();
84 tester.testAllPublicStaticMethods(Objects.class);
85 }
86 }